using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class EE_UI_Bar : MonoBehaviour
{
    public Image bar = null;// 进度条
    public RectTransform rect = null;
    public TextMeshProUGUI txt = null;

    [HideInInspector] public EE_Building_Controller building = null;
    
    private float max = 1;
    private float min = 0;
    private float value = 0.5f;
    private void Awake()
    {
        rect.anchorMin = Vector2.zero;
        rect.anchorMax = Vector2.zero;
    }

    public void SetMin(float v)
    {
        min = v;
        AdjustUI();
    }

    public void SetMax(float v)
    {
        max = v;
        AdjustUI();
    }

    public void SetValue(float v)
    {
        value = v;
        AdjustUI();
    }

    public void AdjustUI()
    {
        TimeSpan timeSpan = building.data.constructionTime - DateTime.Now;
        if (timeSpan.Seconds < 0)
        {
            // todo： 发送建筑升级成功 给服务器 更新数据库表
            building.data.constructing = false;
            // 建造完成, 进度条隐藏
            gameObject.SetActive(false);
        }
        else
        {
            if (timeSpan.TotalDays > 1)
            {
                // TMP 默认不支持中文，做法:
                // 1.右键字体文件->create->TextMeshPro->FontAsset;
                // 2.修改项目默认字体: Edit->Project Settings->TextMeshPro Settings;
                txt.text = timeSpan.ToString(@"dd") + string.Format("天", "utf-8") + timeSpan.ToString(@"hh\:mm\:ss");
            }
            else if (timeSpan.TotalHours > 1)
            {
                txt.text = timeSpan.ToString(@"hh\:mm\:ss");
            }
            else if (timeSpan.TotalMinutes > 1)
            {
                txt.text = timeSpan.ToString(@"mm\:ss");
            }
            else
            {
                txt.text = timeSpan.ToString(@"ss");
            }
            // 进度条
            bar.fillAmount = 1 - (float)timeSpan.TotalSeconds / building.data.upgradeCostSeconds;
        }
    }
}